package FIT::Document::Handler::SameAsAbove;

use FIT::Class;
use FIT::Util::SpecialChars;
use FIT::Document::Transformer;

extends 'FIT::Document::Handler::Base';

# will set it on fixture to store previous values. sneaky
my $Fixture_State_Key = __PACKAGE__.'::previous_values';

sub handle {
    my ($fixture, $cell, @handlers) = @_;
    $self->handle_previous_value($fixture, $cell)
        if $cell->detect_mark($MARKER_SAME_AS_ABOVE);
    $self->set_previous_value($fixture, $cell);
    $self->run_next_handler($fixture, $cell, @handlers);
}

sub handle_previous_value {
    my ($fixture, $cell) = @_;
    die "Previous value has not been set so can't copy value from cell above as there is none or it has been skipped"
        unless $self->exists_previous_value($fixture, $cell);
    my $previous_value = $self->get_previous_value($fixture, $cell);
    transform { Replace element is $previous_value } $cell;
}

sub set_previous_value {
    my ($fixture, $cell) = @_;
    $fixture->{$Fixture_State_Key}->{$cell->column_index} = $cell->copy;
}

sub get_previous_value {
    my ($fixture, $cell) = @_;
    return $fixture->{$Fixture_State_Key}->{$cell->column_index};
}

sub exists_previous_value {
    my ($fixture, $cell) = @_;
    return exists $fixture->{$Fixture_State_Key}->{$cell->column_index};
}


=head1 NAME

FIT::Document::Handler::SameAsAbove - put in this cell a copy of the cell above


=head1 DESCRIPTION

This handler saves you typing by letting you reuse the cell above. The cell
above is copied as it was before all test processing. So you get the cell above
before it was marked or altered in any way.


=cut

